package com.ruoyi.web.controller.irdms;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.InspectionStepEnum;
import com.ruoyi.system.domain.CallRecord;
import com.ruoyi.system.domain.InspectionPoint;
import com.ruoyi.system.domain.InspectionRecord;
import com.ruoyi.system.service.IrdmsService;
import org.aspectj.weaver.loadtime.Aj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.alibaba.fastjson2.JSON.toJSON;
import static com.ruoyi.common.utils.QiNiuYunHttpClientRemote.qnySaveImage;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * @Author: azh
 * Created on 2025-2-25 22:25
 */
@RestController
@RequestMapping("/irdms")
public class InspectionController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(InspectionController.class);

    @Autowired
    private IrdmsService service;


    @GetMapping("/queryInspection")
    public AjaxResult queryInspection() {
        try {
            return AjaxResult.success("SUCCESS", service.queryInspection());
        } catch (Exception e) {
            logger.error("Ops queryInspection. ", e);
            return AjaxResult.error();
        }
    }

    @GetMapping("/queryLineTypeByUsername")
    public TableDataInfo queryLineTypeByUsername(String username) {
        try {
            if (isBlank(username)) {
                return getDataTableFail("username不可以为null");
            }
            List<InspectionPoint> list = service.queryLineTypeByUsername(username);
            logger.info("queryLineTypeByUsername username: {}, result: {}", username, toJSON(list));
            return getDataTable(list);
        } catch (Exception e) {
            logger.error("Ops queryLineTypeByUsername. username:{}", username, e);
            return getDataTableFail("FAIL");
        }
    }

    @PostMapping("/saveInspection")
    public AjaxResult validateInspection(@RequestBody InspectionPoint point) {
        try {
            service.insertInspection(point);
            logger.info("saveInspection: {}", toJSON(point));
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("Ops validateInspection. point: {}", toJSON(point), e);
            return AjaxResult.error();
        }
    }

    @PostMapping("/insertCallRecord")
    public AjaxResult insertCallRecord(@RequestBody CallRecord record) {
        try {
            service.insertCallRecord(record);
            logger.info("insertCallRecord: {}", toJSON(record));
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("Ops insertCallRecord. record: {}", toJSON(record), e);
            return AjaxResult.error();
        }
    }

    @GetMapping("/queryInspectionStep")
    public AjaxResult queryInspectionStep() {
        Map<String, String> map = Arrays.stream(InspectionStepEnum.values())
                .collect(Collectors.toMap(Enum::name, InspectionStepEnum::getDescription));
        return AjaxResult.success(map);
    }

    @PostMapping("/insertInspectionStep")
    public AjaxResult insertInspectionStep(@RequestBody InspectionRecord record) {
        try {
            service.insertInspectRecord(record);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("Ops insertInspectionStep error. record: {}", toJSON(record), e);
            return AjaxResult.error();
        }
    }

    @PostMapping("/updateInspectionStep")
    public AjaxResult updateInspectionStep(@RequestBody InspectionRecord record) {
        try {
            service.updateInspectRecord(record);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("Ops updateInspectionStep error. record: {}", toJSON(record), e);
            return AjaxResult.error();
        }
    }

    @GetMapping("/queryInspectionRecordByState")
    public TableDataInfo queryInspectionRecordByState(String username, int state) {
        try {
            List<InspectionRecord> recordList = service.selectByUsernameAndState(username, state);
            logger.info("selectByUsernameAndState username: {}, recordList: {}", username, toJSON(recordList));
            return getDataTable(recordList);
        } catch (Exception e) {
            logger.error("Ops selectByUsernameAndState error. username: {}, state: {}", username, state, e);
            return getDataTableFail("FAIL");
        }
    }

    @PostMapping("/saveImage")
    public AjaxResult saveImage(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return AjaxResult.error("文件不可为null");
            }
            String url = qnySaveImage(file);
            if (isBlank(url)) {
                return AjaxResult.error("文件上传路径为null");
            }
            logger.info("saveImage 七牛云图片. url: {}", url);
            return AjaxResult.success(url);
        } catch (Exception e) {
            logger.error("Ops saveImage. ", e);
            return AjaxResult.error();
        }
    }
}
